﻿using Duality;
using Duality.Resources;
using OpenTK.Input;

namespace ClientApp
{
	public class ClientAppCorePlugin : CorePlugin
	{
		protected override void OnBeforeUpdate()
		{
			base.OnBeforeUpdate();
			if (DualityApp.ExecContext != DualityApp.ExecutionContext.Editor)
			{
				AiManager.Instance.PrepareAgentUpdates();
				TuioListener.Instance.Update();
				//InputManager.Instance.Update();
				Player.Cherry.Update();
				Player.Lemon.Update();
			}
		}

		protected override void OnAfterUpdate()
		{
			base.OnAfterUpdate();
			// Restart game
			if (DualityApp.Keyboard[Key.Enter]) this.RestartGame();
			// Exit game
			else if (DualityApp.Keyboard[Key.Escape]) DualityApp.Terminate();
		}

		protected override void OnExecContextChanged(DualityApp.ExecutionContext previousContext)
		{
			base.OnExecContextChanged(previousContext);

			// Assure TUIO connection is closed
			if (DualityApp.ExecContext == DualityApp.ExecutionContext.Editor)
				TuioListener.Instance.Disconnect();
			else
				TuioListener.Instance.Connect();
		}
		protected override void OnDisposePlugin()
		{
			base.OnDisposePlugin();
			// Assure TUIO connection is closed
			TuioListener.Instance.Disconnect();
		}

		private void RestartGame()
		{
			Scene.Current.Dispose();
			Player.Cherry.Dispose();
			Player.Lemon.Dispose();
			Scene.Current = GameRes.Data.Gamedata.MainScene_Scene.Res;
		}
	}
}
